/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Ex4_CheckSring;

import java.text.NumberFormat;

/**
 *
 * @author Root
 */
public class Check {

    public Check() {
    }


  /**
   * Format a floating point number.
   * @param num
   * @param decimals
   * @return
   */
  public static String formatNumber(final double num,
          final int decimals) {
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMinimumFractionDigits(decimals);
    nf.setMaximumFractionDigits(decimals);
    return nf.format(num);
  }

  /**
   * Format a floating point number using percentage format
   * @param num
   * @param decimals
   * @return
   */
  public static String formatPercent(final double num, final int decimals) {
    NumberFormat nf = NumberFormat.getPercentInstance();
    nf.setMinimumFractionDigits(decimals);
    nf.setMaximumFractionDigits(decimals);
    return nf.format(num);
  }

  /**
   * Convert an integer to a binary representation
   * @param num
   * @return
   */
  public static String toBinary(final int num) {
    StringBuffer sb = new StringBuffer();
    int d = (int) num / 2;
    int r = num % 2;
    sb.append(r);
    while (d >= 2) {
      r = d % 2;
      sb.append(r);
      d = (int) d / 2;
      //sb.insert(0,r);
    }

    //sb.insert(0,d);
    sb.append(d);
    sb = sb.reverse();

    return sb.toString();
  }

  /**
   * Convert a binary representation to an integer
   * @param bin
   * @return
   */
  public static int toNumber(final String bin) {
    int num = 0;
    int l = bin.length();
    char c = '\u0000';
    for (int i = 0; i < l; i++) {
      c = bin.charAt(l-i-1);
      if (c == '1') {
        num += (int) Math.pow(2, i);
      }
    }
    return num;
  }

  /**
   * Check if str1 is a sub-string of str2
   * @param str1
   * @param str2
   * @return
   */

  public static boolean isSubString(final String str1, final String str2) {
    boolean result = false;
    int len1 = str1.length();
    int len = str2.length();
    char c1 = str1.charAt(0);
    char c;
    for (int i = 0; i < len; i++) {
      c = str2.charAt(i);
      if (c == c1) {
        result = true;
        if (i + len1 <= len) {
          // check remaining
          for (int j = 1; j < len1; j++) {
            if (str1.charAt(j) != str2.charAt(i + j)) {
              result = false;
              i = i + j;
              break;
            }
          }
        } else {
          result = false;
        }

        if (result) {
          // found
          break;
        }
      }
    } // end main for loop

    return result;
  }

  /**
   * Print the content of an array
   */
  public static void printArray(int[] array) {
    System.out.print('<');
    for (int i = 0; i < array.length; i++) {
      // print an element
      System.out.print(array[i]);
      // print comma if not the last
      if (i + 1 < array.length) {
        System.out.print(", ");
      }
    }
    System.out.print('>');
  }

  /**
   * Start a time-stamp in nanosecs
   * @return
   */
  public static long startTimeStamp() {
    return System.nanoTime();
  }

  /**
   * End a time-stamp and returns the duration
   * (used with startTimeStamp() method)
   * @param start
   * @return duration in nanosecs
   */
  public static long endTimeStamp(long start) {
    return (System.nanoTime() - start);
  }

}
